import Vue from "vue";

let vNode = null

export function modalOpen({render, callback}) {
    const body = document.querySelector('body');
    const element = document.createElement('div');
    element.id = 'modal';
    const elementChild = document.createElement('div');
    elementChild.id = 'modal-child'
    element.appendChild(elementChild)
    body.appendChild(element)
    vNode = new Vue({
        render
    })
    vNode.$mount(elementChild)
    const targetInstance = vNode.$children[0];
    callback && callback(targetInstance)
}

export function modalDestroy () {
    const element = document.getElementById('modal')
    if (element) {
        element.remove()
    }
    if (vNode) {
        vNode.$destroy()
        vNode.$el.remove();
    }
}
